<template>
  <div>
    <el-menu
      :default-active="activeIndex"
      class="el-menu-demo"
      mode="horizontal"
      @select="handleSelect"
      background-color="#242424"
      text-color="#f6f6f6"
      active-text-color="#4797f5" router>
      <el-menu-item style="margin-left: 13%">
        <router-link to="/home/homePage">
          <el-image v-bind:src="mylable" class="right"></el-image>
        </router-link>
      </el-menu-item>
      <el-menu-item index="/home/homePage"><i class="fa fa-home" aria-hidden="true"></i> 首页</el-menu-item>
      <el-menu-item index="/type/showByType"><i class="fa fa-navicon" aria-hidden="true"></i> 分类</el-menu-item>
      <el-menu-item index="/label/showByLabel"><i class="fa fa-tags" aria-hidden="true"></i> 标签</el-menu-item>
      <el-menu-item class="center">
        <el-dropdown trigger="click">
          <el-avatar :size="40" :src="user.avatar"></el-avatar>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item v-show="user.status===false" @click.native="login">
              <i class="fa fa-sign-in" aria-hidden="true"></i>登录
            </el-dropdown-item>
            <el-dropdown-item v-show="user.status===true">
              <i class="fa fa-pencil-square-o" aria-hidden="true"></i>修改信息
            </el-dropdown-item>
            <el-dropdown-item v-show="user.status===true" @click.native="signOut">
              <i class="fa fa-sign-out" aria-hidden="true"></i>注销
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-menu-item>
      <el-menu-item class="center" index="/manager" style="font-size: small"><i class="fa fa-cogs" aria-hidden="true" style="margin-right: 3px"></i> 管理</el-menu-item>
    </el-menu>
  </div>
</template>

<script>
import qs from 'qs'
export default {
  inject: ['reload'],
  name: "Top",
  data() {
    return {
      activeIndex: '1',
      mylable: "https://origins-blog.oss-cn-beijing.aliyuncs.com/%E7%BD%91%E7%AB%99%E6%A0%87%E7%AD%BE.png",
      user: this.$store.state.user,
      form: {
        context: '',
      },
      keywords: '',
    };
  },
  methods: {
    handleSelect(key, keyPath) {
    },
    signOut() {
      localStorage.removeItem("user");
      this.$store.commit("signOut");
      this.axios.post("/user/signOut",qs.stringify({
        username: this.user.username
      })).then((response)=>{
      })
      this.reload();
    },
    login() {
      this.$router.push('/login');
    },
  },
  watch: {
    "$store.state.user"() {
      this.user = this.$store.getters.getUser;
    }
  },
  created() {
    if (localStorage.getItem("user") != null) {
      this.user = JSON.parse(localStorage.getItem("user"));
      this.$store.commit("setUser", this.user);
    }
  }
}
</script>

<style scoped>
.el-menu-item {
  font-size: 20px;
}

.el-menu--horizontal > .el-menu-item.is-active {
  border-bottom: 0;
}

.el-menu.el-menu--horizontal {
  border-bottom: 0;
}

.right {
  float: right;
}

.center {
  display: flex;
  align-items: center;
  float: right;
}
</style>
